package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Dish;
import com.itheima.entity.vo.DishVo;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;


    @GetMapping("/list")
    public R<Object> getByCategoryId(Dish dish) {


//        A. 根据分类 ID 查询，查询目前正在启售的菜品列表 (已实现)
//
//        B. 遍历菜品列表，并查询菜品的分类信息及菜品的口味列表
//
//        C. 组装数据 DishDto，并返回

        List<DishVo> byCategoryId = dishService.getByCategoryId(dish.getCategoryId(),dish.getName());
        System.out.println(byCategoryId);

        return byCategoryId == null ? R.error("查询失败"):R.success(byCategoryId);
    }


//    @GetMapping("/list")
//    public R<Object> getByCategoryId(Dish dish) {
//
//        List<Dish> byCategoryId = dishService.getByCategoryId(dish.getCategoryId(),dish.getName());
//        System.out.println(byCategoryId);
//
//    return byCategoryId == null ? R.error("查询失败"):R.success(byCategoryId);
//    }

    /**
     * 修改售卖状态
     * @param
     */
    @PostMapping("/status/{status}")
    public R<Object> updateStatus(@PathVariable("status") Integer status,
                                  @RequestParam("ids") List<Long> ids
                                  ){

        System.out.println("------"+ids);

        if (dishService.updateStatus(ids,status)) {
          return R.success("修改状态成功");
        }
          return R.error("修改状态失败");

    }

    @DeleteMapping()
     public R<Object>  deleteDish(@RequestParam("ids") List<Long> ids){
         System.out.println("--"+ ids);

        if (dishService.deleteDish(ids)) {
         return    R.success("删除成功");
        }
        return  R.error("删除失败");

     }

    @PutMapping()
    public R<Object> update(@RequestBody DishVo  dishVo){
        if (dishService.updateDish(dishVo)) {
          return R.success("修改成功");
        }
          return R.error("修改失败");

    }


   @GetMapping("/{id}")
    public R<Object> getByidDish(@PathVariable Long id){
       DishVo byIdDish = dishService.getByIdDish(id);
       System.out.println("-------"+byIdDish);
       if (byIdDish == null){
        return   R.error("查询失败");

       }
       return R.success(byIdDish);
     }

    @PostMapping()
    public R<Object> addDish(@RequestBody DishVo dish, HttpServletRequest request) {
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        Integer id = (Integer) request.getSession().getAttribute("id");
        long lId = Long.parseLong(String.valueOf(id));
        dish.setCreateUser(lId);
        dish.setUpdateUser(lId);

        if (dishService.addDish(dish) == false) {
            return R.error("添加失败");
        }
        return R.success("添加成功");

    }


    @GetMapping("/page")
    public R<Object> getDish(Integer page,Integer pageSize,String name){

        IPage<DishVo> dish = dishService.getDish(page, pageSize,name);

     return dish == null ? R.error("获取数据失败"):R.success(dish);
    }
}
